<?php
	include("config.php");

	if(isset($_POST['ce']) && isset($_POST['cid']))
	{
		$companyid = $_POST['cid'];
		$companyemail = $_POST['ce'];

		$stmt = $con->prepare('SELECT Company.ID, Company.Email, Company.Name FROM Company WHERE Company.Email = ?');
		$stmt->bind_param('s', $companyemail);
		$stmt->execute();
		
		$result = $stmt->get_result();
		$row = $result->fetch_assoc();

		$count=mysqli_num_rows($result);
		if($count > 0)
		{
			while($row = $result->fetch_assoc())
			{
				if($row['ID'] != $companyid)
				{
					$data["result"] = false;
					$data["message"] = "Company_Email_Taken";
					echo json_encode($data);
					mysqli_close($con);
					exit;
				}
				
			}
		}
	}


	if(isset($_POST['cn']) && isset($_POST['ce']) && !isset($_POST['cid']))
	{
		$companyname = $_POST['cn'];
		$companyemail = $_POST['ce'];

		$stmt = $con->prepare('SELECT Company.Email, Company.Name FROM Company WHERE Company.Email = ? OR Company.Name = ?');
		$stmt->bind_param('ss', $companyemail, $companyname);
		$stmt->execute();
		
		$result = $stmt->get_result();
		$row = $result->fetch_assoc();

		$count=mysqli_num_rows($result);
		if($count > 0)
		{
			while($row = $result->fetch_assoc())
			{
				 if($row['Name'] == $companyname)
				{
					$data["result"] = false;
					$data["message"] = "Company_Name_Taken";
					echo json_encode($data);
					mysqli_close($con);
					exit;
				}
				else if($row['Email'] == $companyemail)
				{
					$data["result"] = false;
					$data["message"] = "Company_Email_Taken";
					echo json_encode($data);
					mysqli_close($con);
					exit;
				}
				
			}
		}
	}

	if(isset($_POST['me']) && !isset($_POST['mid']))
	{
		$manageremail = $_POST['me'];
		
		$stmt = $con->prepare('SELECT User.ID FROM User WHERE User.Login = ?');
		$stmt->bind_param('s', $manageremail);
		$stmt->execute();
		
		$result = $stmt->get_result();

		$count=mysqli_num_rows($result);
		if($count > 0)
		{
			$data["result"] = false;
			$data["message"] = "Manager_Email_Taken";
			echo json_encode($data);
			mysqli_close($con);
			exit;
		}
	}

	if(isset($_POST['me']) && isset($_POST['mid']))
	{
		$manageremail = $_POST['me'];
		$mID = $_POST['mid'];
		
		$stmt = $con->prepare('SELECT User.ID FROM User WHERE User.Login = ?');
		$stmt->bind_param('s', $manageremail);
		$stmt->execute();
		
		$result = $stmt->get_result();

		$count=mysqli_num_rows($result);
		if($count > 0)
		{
			while($row = $result->fetch_assoc())
			{
				if($row['ID'] != $mID)
				{
					$data["result"] = false;
					$data["message"] = "Manager_Email_Taken";
					echo json_encode($data);
					mysqli_close($con);
					exit;
				}
			}
		}
	}

	if(isset($_POST['ee']) && !isset($_POST['eid']))
	{
		$employeemail = $_POST['ee'];
		
		$stmt = $con->prepare('SELECT User.ID FROM User WHERE User.Login = ?');
		$stmt->bind_param('s', $employeemail);
		$stmt->execute();
		
		$result = $stmt->get_result();

		$count=mysqli_num_rows($result);
		if($count > 0)
		{
			$data["result"] = false;
			$data["message"] = "Employee_Email_Taken";
			echo json_encode($data);
			mysqli_close($con);
			exit;
		}
	}

	if(isset($_POST['ee']) && isset($_POST['eid']))
	{
		$employeemail = $_POST['ee'];
		$eID = $_POST['eid'];
		
		$stmt = $con->prepare('SELECT User.ID FROM User WHERE User.Login = ?');
		$stmt->bind_param('s', $employeemail);
		$stmt->execute();
		
		$result = $stmt->get_result();

		$count=mysqli_num_rows($result);
		if($count > 0)
		{
			while($row = $result->fetch_assoc())
			{
				if($row['ID'] != $eID)
				{
					$data["result"] = false;
					$data["message"] = "Employee_Email_Taken";
					echo json_encode($data);
					mysqli_close($con);
					exit;
				}
			}
		}
	}

	$data["result"] = true;
	$data["message"] = "";
	echo json_encode($data);

	mysqli_close($con);

?>